﻿using BrnMall.Manager.Domain.User;
using PetaPoco;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BrnMall.Manager.Manager.User
{
    /// <summary>
    /// 
    /// </summary>
    public static class DirectPaymentManager
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public static DirectPayment GetPaymentByPid(int pid) {
            using (var db = new QDMallDatabase()) {
                return db.SingleOrDefault<DirectPayment>(pid);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="psn"></param>
        /// <returns></returns>
        public static DirectPayment GetPaymentByPSN(string psn)
        {
            using (var db = new QDMallDatabase())
            {
                return db.SingleOrDefault<DirectPayment>("select top 1 * from bma_directpayment where psn=@0",psn);
            }
        }
        public static int AddDirectPayment(DirectPayment pay) {

            using (var db = new QDMallDatabase()) {
                db.Insert(pay);
                return pay.Pid;
            }
        }
        public static void UpdateDirectPayment(DirectPayment pay) {
            using (var db = new QDMallDatabase()) {
                db.Update(pay);
            }
        }
        public static void UpdateDirectPaymentStatus(int pid,int status)
        {
            using (var db = new QDMallDatabase())
            {
                db.Execute("update bma_directpayment set orderstate=@0 where Pid=@1", status, pid);
            }
        }
        public static Page<DirectPayment> GetUserPage(int uid,long page,long itemsPerPage=8)
        {
            using (var db = new QDMallDatabase()) {
                return db.Page<DirectPayment>(page, itemsPerPage, "select * from bma_directpayment where uid=@0",uid);
            }
        }
        public static Page<DirectPayment> GetStorePage(string condition, long page, long itemsPerPage = 8)
        {
            using (var db = new QDMallDatabase())
            {
                var sql = new Sql();
                sql.Select("*");
                sql.From("bma_directpayment");
                sql.Where(condition);
                sql.OrderBy("AddTime DESC");
                return db.Page<DirectPayment>(page, itemsPerPage, sql);
            }
        }
        public static void PayOrder(int pid, string psn) {
            using (var db = new QDMallDatabase()) {
                db.Execute("update bma_DirectPayment set orderstate=1,paysn=@0,paytime=getdate() where pid=@1", psn, pid);
            }
        }

        public static void ReturnCoupon(int couponId)
        {
            using (var db = new QDMallDatabase())
            {
                db.Execute(
                    "UPDATE [bma_coupons] SET [oid]=0,[usetime]='1900-1-1 00:00:00',[useip]='' WHERE [couponid]=@0",
                    couponId);
            }
        }
    }
}
